home *** CD-ROM | disk | FTP | other *** search
/ 3D GFX / 3D GFX.iso / amiutils / i_l / irit5 / cagd_lib / cagdbbox.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-31  |  12.7 KB  |  270 lines

  1. /******************************************************************************
  2. * CagdBbox.c - Handle freeform cuves and surfaces bounding boxes.          *
  3. *******************************************************************************
  4. * Written by Gershon Elber, Jan. 92.                          *
  5. ******************************************************************************/
  6.  
  7. #include "cagd_loc.h"
  8.  
  9. /*****************************************************************************
  10. * DESCRIPTION:                                                               M
  11. * Computes a bounding box for a freeform curve.                     M
  12. *                                                                            *
  13. * PARAMETERS:                                                                M
  14. *   Crv:      To compute a bounding box for.                                 M
  15. *   BBox:     Where bounding information is to be saved.                     M
  16. *                                                                            *
  17. * RETURN VALUE:                                                              M
  18. *   void                                                                     M
  19. *                                                                            *
  20. * KEYWORDS:                                                                  M
  21. *   CagdCrvBBox, bbox, bounding box                                          M
  22. *****************************************************************************/
  23. void CagdCrvBBox(CagdCrvStruct *Crv, CagdBBoxStruct *BBox)
  24. {
  25.     CagdCrvStruct
  26.     *E3Crv = CagdCoerceCrvTo(Crv, CAGD_PT_E3_TYPE);
  27.     int Length = E3Crv -> Length;
  28.     CagdRType
  29.     **Points = E3Crv -> Points;
  30.  
  31.     CagdPointsBBox(Points, Length, BBox);
  32.  
  33.     CagdCrvFree(E3Crv);
  34. }
  35.  
  36. /*****************************************************************************
  37. * DESCRIPTION:                                                               M
  38. * Computes a bounding box for a list of freeform curves.             M
  39. *                                                                            *
  40. * PARAMETERS:                                                                M
  41. *   Crvs:     To compute a bounding box for.                                 M
  42. *   BBox:     Where bounding information is to be saved.                     M
  43. *                                                                            *
  44. * RETURN VALUE:                                                              M
  45. *   void                                                                     M
  46. *                                                                            *
  47. * KEYWORDS:                                                                  M
  48. *   CagdCrvListBBox, bbox, bounding box                                      M
  49. *****************************************************************************/
  50. void CagdCrvListBBox(CagdCrvStruct *Crvs, CagdBBoxStruct *BBox)
  51. {
  52.     CAGD_RESET_BBOX(BBox);
  53.  
  54.     for ( ; Crvs != NULL; Crvs = Crvs -> Pnext) {
  55.     CagdBBoxStruct TmpBBox;
  56.  
  57.     CagdCrvBBox(Crvs, &TmpBBox);
  58.     CagdMergeBBox(BBox, &TmpBBox);
  59.     }
  60. }
  61.  
  62. /*****************************************************************************
  63. * DESCRIPTION:                                                               M
  64. * Computes a bounding box for a freeform surface.                 M
  65. *                                                                            *
  66. * PARAMETERS:                                                                M
  67. *   Srf:      To compute a bounding box for.                                 M
  68. *   BBox:     Where bounding information is to be saved.                     M
  69. *                                                                            *
  70. * RETURN VALUE:                                                              M
  71. *   void                                                                     M
  72. *                                                                            *
  73. * KEYWORDS:                                                                  M
  74. *   CagdSrfBBox, bbox, bounding box                                          M
  75. *****************************************************************************/
  76. void CagdSrfBBox(CagdSrfStruct *Srf, CagdBBoxStruct *BBox)
  77. {
  78.     CagdSrfStruct
  79.     *E3Srf = CagdCoerceSrfTo(Srf, CAGD_PT_E3_TYPE);
  80.     int Length = E3Srf -> ULength * E3Srf -> VLength;
  81.     CagdRType
  82.     **Points = E3Srf -> Points;
  83.  
  84.     CagdPointsBBox(Points, Length, BBox);
  85.  
  86.     CagdSrfFree(E3Srf);
  87. }
  88.  
  89. /*****************************************************************************
  90. * DESCRIPTION:                                                               M
  91. * Computes a bounding box for a list of freeform surfaces.             M
  92. *                                                                            *
  93. * PARAMETERS:                                                                M
  94. *   Srfs:     To compute a bounding box for.                                 M
  95. *   BBox:     Where bounding information is to be saved.                     M
  96. *                                                                            *
  97. * RETURN VALUE:                                                              M
  98. *   void                                                                     M
  99. *                                                                            *
  100. * KEYWORDS:                                                                  M
  101. *   CagdSrfListBBox, bbox, bounding box                                      M
  102. *****************************************************************************/
  103. void CagdSrfListBBox(CagdSrfStruct *Srfs, CagdBBoxStruct *BBox)
  104. {
  105.     CAGD_RESET_BBOX(BBox);
  106.  
  107.     for ( ; Srfs != NULL; Srfs = Srfs -> Pnext) {
  108.     CagdBBoxStruct TmpBBox;
  109.  
  110.     CagdSrfBBox(Srfs, &TmpBBox);
  111.     CagdMergeBBox(BBox, &TmpBBox);
  112.     }
  113. }
  114.  
  115. /*****************************************************************************
  116. * DESCRIPTION:                                                               M
  117. * Computes a bounding box for a set of control points.                     M
  118. *                                                                            *
  119. * PARAMETERS:                                                                M
  120. *   Points:     To compute bounding box for.                                 M
  121. *   Length:     Length of vectors of Points array.                           M
  122. *   BBox:       Where bounding information is to be saved.                   M
  123. *                                                                            *
  124. * RETURN VALUE:                                                              M
  125. *   void                                                                     M
  126. *                                                                            *
  127. * KEYWORDS:                                                                  M
  128. *   CagdPointsBBox, bbox, bounding box                                       M
  129. *****************************************************************************/
  130. void CagdPointsBBox(CagdRType **Points, int Length, CagdBBoxStruct *BBox)
  131. {
  132.     int i;
  133.  
  134.     CAGD_RESET_BBOX(BBox);
  135.  
  136.     for (i = 0; i < Length; i++) {
  137.     if (BBox -> Min[0] > Points[X][i])
  138.         BBox -> Min[0] = Points[X][i];
  139.     if (BBox -> Min[1] > Points[Y][i])
  140.         BBox -> Min[1] = Points[Y][i];
  141.     if (BBox -> Min[2] > Points[Z][i])
  142.         BBox -> Min[2] = Points[Z][i];
  143.  
  144.     if (BBox -> Max[0] < Points[X][i])
  145.         BBox -> Max[0] = Points[X][i];
  146.     if (BBox -> Max[1] < Points[Y][i])
  147.         BBox -> Max[1] = Points[Y][i];
  148.     if (BBox -> Max[2] < Points[Z][i])
  149.         BBox -> Max[2] = Points[Z][i];
  150.     }
  151. }
  152.  
  153. /*****************************************************************************
  154. * DESCRIPTION:                                                               M
  155. * Merges (union) two bounding boxes into one, in place.                 M
  156. *                                                                            *
  157. * PARAMETERS:                                                                M
  158. *   DestBBox:    One BBox operand as well as the result.                     M
  159. *   SrcBBox:     Second BBox operand.                                        M
  160. *                                                                            *
  161. * RETURN VALUE:                                                              M
  162. *   void                                                                     M
  163. *                                                                            *
  164. * KEYWORDS:                                                                  M
  165. *   CagdMergeBBox, bbox, bounding box                                        M
  166. *****************************************************************************/
  167. void CagdMergeBBox(CagdBBoxStruct *DestBBox, CagdBBoxStruct *SrcBBox)
  168. {
  169.     int i;
  170.  
  171.     for (i = 0; i < 3; i++) {
  172.     if (DestBBox -> Min[i] > SrcBBox -> Min[i])
  173.         DestBBox -> Min[i] = SrcBBox -> Min[i];
  174.     if (DestBBox -> Max[i] < SrcBBox -> Max[i])
  175.         DestBBox -> Max[i] = SrcBBox -> Max[i];
  176.     }
  177. }
  178.  
  179. /*****************************************************************************
  180. * DESCRIPTION:                                                               M
  181. * Computes a min max bound on a curve in a given axis.                 M
  182. *   The curve is not coerced to anything and the given axis is tested        M
  183. * directly where 0 is the W axis and 1, 2, 3 are the X, Y, Z etc.         M
  184. *                                                                            *
  185. * PARAMETERS:                                                                M
  186. *   Crv:      To test for minimum/maximum.                                   M
  187. *   Axis:     0 for W, 1 for X, 2 for Y etc.                                 M
  188. *   Min:      Where minimum found value should be place.                     M
  189. *   Max:      Where maximum found value should be place.                     M
  190. *                                                                            *
  191. * RETURN VALUE:                                                              M
  192. *   void                                                                     M
  193. *                                                                            *
  194. * KEYWORDS:                                                                  M
  195. *   CagdCrvMinMax, bbox, bounding box, minimum, maximum                      M
  196. *****************************************************************************/
  197. void CagdCrvMinMax(CagdCrvStruct *Crv,
  198.            int Axis,
  199.            CagdRType *Min,
  200.            CagdRType *Max)
  201. {
  202.     CagdBType
  203.     IsNotRational = !CAGD_IS_RATIONAL_CRV(Crv);
  204.     int i,
  205.     Length = Crv -> Length,
  206.     MaxCoord = CAGD_NUM_OF_PT_COORD(Crv -> PType);
  207.     CagdRType
  208.     *Pts = Crv -> Points[Axis],
  209.     *WPts = IsNotRational ? NULL : Crv -> Points[0];
  210.  
  211.     if ((Axis == 0 && IsNotRational) ||    (Axis > MaxCoord))
  212.     CAGD_FATAL_ERROR(CAGD_ERR_WRONG_CRV);
  213.  
  214.     for (i = 0, *Min = INFINITY, *Max = -INFINITY; i < Length; i++) {
  215.     CagdRType
  216.         V = WPts ? Pts[i] / WPts[i] : Pts[i];
  217.  
  218.     if (*Max < V)
  219.         *Max = V;
  220.     if (*Min > V)
  221.         *Min = V;
  222.     }
  223. }
  224.  
  225. /*****************************************************************************
  226. * DESCRIPTION:                                                               M
  227. * Computes a min max bound on a surface in a given axis.             M
  228. *   The surface is not coerced to anything and the given axis is tested      M
  229. * directly where 0 is the W axis and 1, 2, 3 are the X, Y, Z etc.         M
  230. *                                                                            *
  231. * PARAMETERS:                                                                M
  232. *   Srf:      To test for minimum/maximum.                                   M
  233. *   Axis:     0 for W, 1 for X, 2 for Y etc.                                 M
  234. *   Min:      Where minimum found value should be place.                     M
  235. *   Max:      Where maximum found value should be place.                     M
  236. *                                                                            *
  237. * RETURN VALUE:                                                              M
  238. *   void                                                                     M
  239. *                                                                            *
  240. * KEYWORDS:                                                                  M
  241. *   CagdSrfMinMax, bbox, bounding box, minimum, maximum                      M
  242. *****************************************************************************/
  243. void CagdSrfMinMax(CagdSrfStruct *Srf,
  244.            int Axis,
  245.            CagdRType *Min,
  246.            CagdRType *Max)
  247. {
  248.     CagdBType
  249.     IsNotRational = !CAGD_IS_RATIONAL_SRF(Srf);
  250.     int i,
  251.     Length = Srf -> ULength * Srf -> VLength,
  252.     MaxCoord = CAGD_NUM_OF_PT_COORD(Srf -> PType);
  253.     CagdRType
  254.     *Pts = Srf -> Points[Axis],
  255.     *WPts = IsNotRational ? NULL : Srf -> Points[0];
  256.  
  257.     if ((Axis == 0 && IsNotRational) ||    (Axis > MaxCoord))
  258.     CAGD_FATAL_ERROR(CAGD_ERR_WRONG_SRF);
  259.  
  260.     for (i = 0, *Min = INFINITY, *Max = -INFINITY; i < Length; i++) {
  261.     CagdRType
  262.         V = WPts ? Pts[i] / WPts[i] : Pts[i];
  263.  
  264.     if (*Max < V)
  265.         *Max = V;
  266.     if (*Min > V)
  267.         *Min = V;
  268.     }
  269. }
  270.